<template>

  <el-main>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="酒店名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="酒店星级" prop="level">
        <el-select v-model="ruleForm.level" placeholder="请选择酒店星级">
          <el-option v-for="l in this.levelData" :label="l.name" :value="l.level"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-select v-model="ruleForm.brand" placeholder="请选择品牌">
          <el-option v-for="l in this.brandData" :label="l.name" :value="l.level"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地理位置经度" prop="invdo">
        <el-input v-model="ruleForm.invdo"></el-input>
      </el-form-item>
      <el-form-item label="地理位置纬度" prop="outvdo">
        <el-input v-model="ruleForm.outvdo"></el-input>
      </el-form-item>
      <el-form-item label="最低价格" prop="minprice">
        <el-input v-model="ruleForm.minprice"></el-input>-
      </el-form-item>
      <el-form-item label="最高价格" prop="maxprice">
        <el-input v-model="ruleForm.maxprice"></el-input>
      </el-form-item>

      <el-form-item label="视频介绍" prop="vido">
        <el-upload
            class="avatar-uploader"
            action="http://api.com/hotel/tofile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="this.ruleForm.vido" width="120" height="100" :src="'http://api.com/'+this.ruleForm.vido" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>


  </el-main>
</template>

<script>
export default {
  data() {
    return{
      ruleForm: {
          vido:null
      },
      rules: {
        name: [
          { required: true, message: '请输入酒店名称', trigger: 'blur' },
        ],
        level: [
          { required: true, message: '请选择酒店星级', trigger: 'blur' },
        ],
        brand: [
          { required: true, message: '请选择你酒店品牌', trigger: 'blur' },
        ],
        invdo: [
          { required: true, message: '请输入当前位置经度', trigger: 'blur' },
        ],
        outvdo: [
          { required: true, message: '请输入当前位置纬度', trigger: 'blur' },
        ],
        minprice: [
          { required: true, message: '请输入最低价格', trigger: 'blur' },
        ],
        maxprice: [
          { required: true, message: '请输入最高价格', trigger: 'blur' },
        ],
        vido:[
          { required: true, message: '不可为空', trigger: 'blur' },
        ]
      },
      levelData:[{
        name:'经济型',
        level:'jinji'
      },{
        name:'商务型',
        level:'shangwu'
      },{
        name:'临时型',
        level:'linshi'
      }],
      brandData:[{
        name:'如家',
        level:'rujia'
      },{
        name:'七星',
        level:'qixing'
      },{
        name:'一夜',
        level:'yiye'
      }]

    }
  },
  methods:{
    //去发布
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post(this.$baseUrl+"/hotel/add",this.ruleForm).then(res=>{
            console.log("发布酒店信息:", res);
            if (res.data.errCode == 500) {
              this.$message.error(res.data.message);
            } else {
              this.$message.success("发布成功");
              this.$router.push("/hotel");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //上传
    handleAvatarSuccess(res, file) {
      this.ruleForm.vido = res.fileName;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 4;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 4MB!');
      }
      return isJPG && isLt2M;
    }
  },
  created() {

  }
}
</script>

<style scoped>

</style>